package com.ami12.shopping.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ami12.shopping.pojo.User;
import com.ami12.shopping.service.UserService;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Controller
@RequestMapping("/index")
public class IndexController {
    @Autowired
    private UserService userService;

    /**
     * 根据id查找用户信息
     * @param request
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/showUser")
    public String toIndex(HttpServletRequest request,Model model) throws Exception {
        int userId = Integer.parseInt(request.getParameter("id"));
        System.out.println(userId);
        User user = userService.selectByPrimaryKey(userId);
        // hiSystem.out.println(user);
        model.addAttribute("user", user);
        return "showUser";
    }

    /**
     * 用户注册
     * @param username
     * @param password
     * @param model
     * @return
     */
    @RequestMapping("/register")
    public String register(@RequestParam("uname") String username,@RequestParam("upwd") String password,Model model) throws Exception {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setRole(0);
        user.setStatus(0);
        user.setCreate_time(new Date().getTime());
        user.setLast_login_time(new Date().getTime());
        Boolean result = userService.insertUser(user);
        if(result){
            model.addAttribute("message","注册成功");
            return "home";
        }else{
            model.addAttribute("message","注册失败");
            return "home";
        }

    }


    /**
     * 验证账号密码是否正确
     * @param username
     * @param password
     * @param model
     * @return
     * @throws Exception
     */
    @RequestMapping("/login")
    public String checkLogin(@RequestParam("uname") String username,@RequestParam("upwd") String password,Model model) throws Exception {
        Map<String,String> map= new HashMap<String,String>();
        map.put("username",username);
        map.put("password",password);
        String str = userService.checkLogin(map);
        if(str != null){
            model.addAttribute("message","登陆成功！");
            return "home";
        }else{
            model.addAttribute("message","登陆失败！");
            return "home";
        }
    }

    /**
     * 普通的HttpServlet输出内容到页面
     * @param request
     * @param response
     */
    @RequestMapping("/test")
    public void test(HttpServletRequest request, HttpServletResponse response){
        try {
            PrintWriter pw = response.getWriter();
            pw.println("OK?");
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/staticResource")
    public String testStaticResource(){
        return "staticResource";
    }
}
